// 定义点击区域类型
export type ClickArea = {
  x: number // 横坐标
  y: number // 纵坐标
  width: number // 点击区域宽度
  height: number // 点击区域高度
  name: string // 对象名称/标识符
}

// 定义系统类型枚举
export enum SystemType {
  HYDRAULIC = 'hydraulic',
  FUEL = 'fuel',
  AIR = 'air',
  ELECTRIC = 'electric'
}

export class ClickHandler {
  private systemType: SystemType
  private clickAreas: ClickArea[]
  private currentArea: ClickArea | null = null

  constructor(systemType: SystemType, clickAreas: ClickArea[]) {
    this.systemType = systemType
    this.clickAreas = clickAreas
  }

  // 检查点击是否在区域内
  private isInClickArea(clickX: number, clickY: number, area: ClickArea): boolean {
    return (
      clickX >= area.x &&
      clickX <= area.x + area.width &&
      clickY >= area.y &&
      clickY <= area.y + area.height
    )
  }

  // 根据点击位置找到对应的区域
  public findClickedArea(x: number, y: number): string | null {
    for (const area of this.clickAreas) {
      if (this.isInClickArea(x, y, area)) {
        this.currentArea = area
        return area.name
      }
    }
    this.currentArea = null
    return null
  }

  // 根据点击位置找到对应的区域索引
  public findClickedAreaIndex(x: number, y: number): number {
    for (let i = 0; i < this.clickAreas.length; i++) {
      if (this.isInClickArea(x, y, this.clickAreas[i])) {
        return i
      }
    }
    return -1
  }

  // 获取当前激活的区域
  public getCurrentArea(): ClickArea | null {
    return this.currentArea
  }

  // 获取系统类型
  public getSystemType(): SystemType {
    return this.systemType
  }

  // 添加新的点击区域
  public addClickArea(area: ClickArea): void {
    this.clickAreas.push(area)
  }

  // 移除点击区域
  public removeClickArea(areaName: string): void {
    this.clickAreas = this.clickAreas.filter((area) => area.name !== areaName)
  }

  // 获取所有点击区域
  public getAllAreas(): ClickArea[] {
    return this.clickAreas
  }
}
